//
//  MainTabBarController.m
//  星期一
//
//  Created by 来定MAC on 15/7/20.
//  Copyright (c) 2015年 LD. All rights reserved.
//

#import "MainTabBarController.h"
#import "CustomTabView.h"
#import "RemandView.h"
#import <AFNetworking.h>
#import "Common.h"
#import <MJExtension.h>
#import "StartUpModel.h"

@interface MainTabBarController ()<CustomTabViewDelegate>

@property(nonatomic,strong)RemandView *recommandView;//推荐页面
@property(nonatomic,strong)CustomTabView *tabbarView;

@end

@implementation MainTabBarController

#pragma mark --life cycle Area
- (void)viewDidLoad {
    [super viewDidLoad];

    //1. 开始信息获取
    [self loadStartupMessage];
    //2. 视图控件加载
    [self.view addSubview:self.tabbarView];
    [self.view addSubview:self.recommandView];
    
    [self loadSubviewLayout];
    [self setItemCon];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)loadSubviewLayout{
    
    self.recommandView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[imageView]-0-|" options:0 metrics:nil views:@{@"imageView":self.recommandView}]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[imageView]-0-|" options:0 metrics:nil views:@{@"imageView":self.recommandView}]];
    
    [UIView animateWithDuration:5 animations:^{
        self.recommandView.transform = CGAffineTransformMakeScale(1.2f, 1.2f);
        self.recommandView.alpha = 0.8;
    } completion:^(BOOL finished) {
        [self.recommandView removeFromSuperview];
    }];
    
    [self.tabbarView setFrame:self.tabBar.frame];

}

#pragma mark --networking Area
- (void)loadStartupMessage{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *url_str = @"http://apiv6.ixingji.com/ixingji/venus/startup.json?";
    [manager GET:url_str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        LXLog(@"启动数据载入成功");
        StartUpModel *startModel = [StartUpModel objectWithKeyValues:responseObject];
        NSNumber *mid = [NSNumber numberWithInteger:startModel.attachment.mid];
        //发送通知(给主页控制器)
        [[NSNotificationCenter defaultCenter] postNotificationName:@"downloadRemandImg" object:mid];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        LXLog(@"首页数据载入失败 : %@",error);
    }];
    
}

#pragma mark --private method Area
- (void)setItemCon{
    UIStoryboard *homeSB = [UIStoryboard storyboardWithName:@"HomeSB"  bundle:nil];
    UIViewController *homeCon = homeSB.instantiateInitialViewController;
    
    UIStoryboard *buyChoiceSB = [UIStoryboard storyboardWithName:@"BuyChoiceSB" bundle:nil];
    UIViewController *buyChoiceCon = buyChoiceSB.instantiateInitialViewController;
    
    UIStoryboard *buySB = [UIStoryboard storyboardWithName:@"BuySB" bundle:nil];
    UIViewController *buyCon = buySB.instantiateInitialViewController;
    
    UIStoryboard *squareSB = [UIStoryboard storyboardWithName:@"SquareSB" bundle:nil];
    UIViewController *squareCon = squareSB.instantiateInitialViewController;
    
    UIStoryboard *mineSB = [UIStoryboard storyboardWithName:@"MineSB" bundle:nil];
    UIViewController *mineCon = mineSB.instantiateInitialViewController;
    
    NSArray *conArr = [NSArray arrayWithObjects:homeCon,buyChoiceCon,buyCon,squareCon,mineCon,nil];
    [self setViewControllers:conArr animated:YES];
    
}

#pragma mark --delegate & datasource Area
- (void)customTableWithBarItemTag:(NSInteger)TabitemTag{
    [self setSelectedIndex:TabitemTag - 1];
}

#pragma mark --event action Area
- (void)clickSayHelloBtn{}
#pragma mark --lazy load Area
- (RemandView *)recommandView{
    if (_recommandView == nil) {
        
        _recommandView = [[RemandView alloc] init];
    
    }
    return _recommandView;
}
- (CustomTabView *)tabbarView{
    if (_tabbarView == nil) {
        _tabbarView = [[CustomTabView alloc] init];
        _tabbarView.delegate = self;
        [_tabbarView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    }
    return _tabbarView;
}
@end
